{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\ProgramData\\Anaconda3\\lib\\site-packages\\h5py\\__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
      "  from ._conv import register_converters as _register_converters\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting C:\\Users\\zdwxx\\Downloads\\Compressed\\MNIST_data\\train-images-idx3-ubyte.gz\n",
      "Extracting C:\\Users\\zdwxx\\Downloads\\Compressed\\MNIST_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting C:\\Users\\zdwxx\\Downloads\\Compressed\\MNIST_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting C:\\Users\\zdwxx\\Downloads\\Compressed\\MNIST_data\\t10k-labels-idx1-ubyte.gz\n",
      "WARNING:tensorflow:From <ipython-input-2-95ef7cc22c8a>:61: arg_max (from tensorflow.python.ops.gen_math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use `argmax` instead\n",
      "准确率是 0.098\n",
      "INFO:tensorflow:Restoring parameters from c:/model/example/my_mnist_net.ckpt\n",
      "准确率是 0.9057\n"
     ]
    }
   ],
   "source": [
    "# 载入数据集\n",
    "mnist = input_data.read_data_sets(r\"C:\\Users\\zdwxx\\Downloads\\Compressed\\MNIST_data\", one_hot=True)\n",
    "\n",
    "# 定义每个批次的大小\n",
    "batch_size = 100\n",
    "# 计算一共有多少个批次\n",
    "n_batch = mnist.train.num_examples // batch_size\n",
    "\n",
    "def varible_summaries(var):\n",
    "    \n",
    "    with tf.name_scope(\"summary\"):\n",
    "        \n",
    "        mean = tf.reduce_mean(var)\n",
    "        tf.summary.scalar(\"mean\", mean) # 平均值\n",
    "        with tf.name_scope(\"stddev\"):\n",
    "            stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))\n",
    "        tf.summary.scalar(\"stddev\", stddev) # 标准差\n",
    "        tf.summary.scalar(\"max\", tf.reduce_max(var)) #最大值\n",
    "        tf.summary.scalar(\"min\", tf.reduce_min(var)) # 最小值\n",
    "        tf.summary.histogram(\"histogram\", var) # 直方图\n",
    "\n",
    "# 命名空间\n",
    "with tf.name_scope(\"input\"):\n",
    "    # 定义两个placeholder\n",
    "    x = tf.placeholder(tf.float32, [None, 784], name=\"x-input\")\n",
    "    y = tf.placeholder(tf.float32, [None, 10], name=\"y-input\")\n",
    "\n",
    "with tf.name_scope(\"layer\"):\n",
    "    #创建一个简单的神经网络\n",
    "    with tf.name_scope(\"wights\"):\n",
    "        W = tf.Variable(tf.zeros([784, 10]), name=\"W\")\n",
    "        varible_summaries(W)\n",
    "        \n",
    "    with tf.name_scope(\"biases\"):\n",
    "        b = tf.Variable(tf.zeros([10]), name=\"b\")\n",
    "        varible_summaries(b)\n",
    "        \n",
    "    with tf.name_scope(\"wx_plus_b\"):\n",
    "        wx_plus_b = tf.matmul(x, W) + b\n",
    "        \n",
    "    with tf.name_scope(\"softmax\"):\n",
    "        prediction = tf.nn.softmax(wx_plus_b) # 预测值\n",
    "\n",
    "# 二次代价函数\n",
    "\n",
    "# loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))\n",
    "with tf.name_scope(\"loss\"):\n",
    "    loss = tf.reduce_mean(tf.square(y - prediction))\n",
    "    tf.summary.scalar(\"loss\", loss)\n",
    "    \n",
    "# 梯度下降法\n",
    "with tf.name_scope(\"train\"):\n",
    "    train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)\n",
    "\n",
    "# 初始化变量\n",
    "init = tf.global_variables_initializer()\n",
    "\n",
    "with tf.name_scope(\"accuracy\"):\n",
    "    # 结果存放在一个布尔型列表中\n",
    "    with tf.name_scope(\"correct_prediction\"):\n",
    "        correct_prediction = tf.equal(tf.arg_max(y, 1), tf.arg_max(prediction, 1)) #arg_max返回1维张量中最大的值所在的位置\n",
    "    # 求准确率\n",
    "    with tf.name_scope(\"accuracy\"):\n",
    "        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))#cast转换类型，True->1.0, False->0.0\n",
    "        tf.summary.scalar(\"accuracy\", accuracy)\n",
    "\n",
    "# 合并所有的summary\n",
    "merged = tf.summary.merge_all()\n",
    "\n",
    "saver = tf.train.Saver()\n",
    "        \n",
    "with tf.Session() as sess:\n",
    "    \n",
    "    sess.run(init)\n",
    "        \n",
    "    acc = sess.run(accuracy, feed_dict={x:mnist.test.images, y:mnist.test.labels})\n",
    "    print(\"准确率是\", acc)\n",
    "    saver.restore(sess, \"c:/model/example/my_mnist_net.ckpt\")\n",
    "    acc = sess.run(accuracy, feed_dict={x:mnist.test.images, y:mnist.test.labels})\n",
    "    print(\"准确率是\", acc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
